package com.wondersgroup.insurance.dnuilearn.recyclerview.layoutmanager;

import android.graphics.Canvas;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.wondersgroup.insurance.dnuilearn.R;
import com.wondersgroup.insurance.dnuilearn.recyclerview.adapter.UniversalAdapter;
import com.wondersgroup.insurance.dnuilearn.recyclerview.adapter.ViewHolder;
import com.wondersgroup.insurance.dnuilearn.recyclerview.swipe.extend.ItemTouchHelper;

import java.util.List;

/**
 * @author 黄赛赛
 * @version V1.0
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}()
 * @date 2017/6/29 0029 23:33
 */
public class RecyclerCardActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    private UniversalAdapter<SwipeCardBean> adapter;
    private List<SwipeCardBean> mData;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        CardConfig.initConfig(getApplicationContext());
        recyclerView = new RecyclerView(this);
        recyclerView.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));

        recyclerView.setLayoutManager(new SwipeCardManager(this));
        setContentView(recyclerView);

        mData = SwipeCardBean.initDatas();
        adapter = new UniversalAdapter<SwipeCardBean>(this, mData, R.layout.item_swipe_card) {
            @Override
            public void convert(ViewHolder var1, SwipeCardBean var2) {
                var1.setText(R.id.tvName, var2.getName());
                var1.setText(R.id.tvPrecent, var2.getPostition() + "/" + mData.size());
                Glide.with(RecyclerCardActivity.this)
                        .load(var2.getUrl())
                        .into((ImageView) var1.getView(R.id.iv));
            }
        };
        recyclerView.setAdapter(adapter);

        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(new MyItemTouchHelperCallBack());
        itemTouchHelper.attachToRecyclerView(recyclerView);
    }


    class MyItemTouchHelperCallBack extends ItemTouchHelper.Callback{


        @Override
        public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
            int swipeFlags = ItemTouchHelper.LEFT |ItemTouchHelper.RIGHT|ItemTouchHelper.UP|ItemTouchHelper.DOWN;
            return  makeMovementFlags(0,swipeFlags);
        }

        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            return false;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
            int fromPosition = viewHolder.getAdapterPosition();
            SwipeCardBean removeItem = mData.remove(fromPosition);
            mData.add(0,removeItem);
            adapter.notifyItemMoved(fromPosition,0);

        }

        @Override
        public float getMoveThreshold(RecyclerView.ViewHolder viewHolder) {
            return 0.6f;
        }

        @Override
        public long getAnimationDuration(RecyclerView recyclerView, int animationType, float animateDx, float animateDy) {
            return 100;
        }
       @Override
        public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {

            double maxDistance = recyclerView.getWidth() * 0.5f;
            double distance = Math.sqrt(dX * dX + dY * dY);
            double fraction = distance / maxDistance;
            if (fraction > 1) {
                fraction = 1;
            }
            int itemCount = recyclerView.getChildCount();
            for (int i = 0; i < itemCount; i++) {
                View child = recyclerView.getChildAt(i);
                int level = itemCount - i - 1;
                if (level >= 0) {
                    if (level < CardConfig.MAX_SHOW_COUNT - 1) {
                        child.setTranslationY((float) (CardConfig.TRANS_Y_GAP * level - fraction * CardConfig.TRANS_Y_GAP));
                        child.setScaleX((float) (1 - CardConfig.SCALE_GAP * level + fraction * CardConfig.SCALE_GAP));
                        child.setScaleY((float) (1 - CardConfig.SCALE_GAP * level + fraction * CardConfig.SCALE_GAP));
//                    view.setAlpha((float) (1 - 0.1 * level + fraction *CardConfig.SCALE_GAP));
                    } else if (level == CardConfig.MAX_SHOW_COUNT - 1) { // 控制的是最下层的Item
                        child.setTranslationY((float) (CardConfig.TRANS_Y_GAP * (level - 1)));
                        child.setScaleX((float) (1 - CardConfig.SCALE_GAP * (level - 1)));
                        child.setScaleY((float) (1 - CardConfig.SCALE_GAP * (level - 1)));
                    }
                }
            }
             super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
       }
    }



}
